﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using NekoVampire.TwitterClient.Twitter;
using NekoVampire.Extension;

namespace NekoVampire.TwitterClient.Controls
{
    public partial class TwitterStatusView : UserControl
    {
        private Status status;

        public Status Status{
            set
            {
                if (value != null)
                {
                    status = value;

                    nameLabel.Text = status.user.screenName + " / " + status.user.name;
                    dateLabel.Text = status.createdAt.ToShortDateString() + " " + status.createdAt.ToLongTimeString();

                    SetLinkLabel(status.text);
                }                
            }
            get
            {
                return status;
            }
        }

        private Image _image;
        public Image Image
        {
            set
            {
                _image = value;
                userPicture.Image = _image == null ? null : _image.GetResizedImage(48, 48, true);
            }
            get
            {
                return _image;
            }
        }

        public Color ForeColor
        {
            set
            {
                nameLabel.ForeColor = value;
            }
            get
            {
                return nameLabel.ForeColor;
            }
        }


        public TwitterStatusView()
        {
            InitializeComponent();
        }

        private void SetLinkLabel(string text)
        {
            linkLabel1.Text = text;

            linkLabel1.Links.Clear();
            Regex regex = new Regex(@"@([0-9a-zA-Z_]+)");
            foreach (Match match in regex.Matches(text))
            {
                linkLabel1.Links.Add(match.Index + 1, match.Length - 1, match.Groups[1].Value);
            }
            regex = new Regex("https?://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+");
            foreach (Match match in regex.Matches(text))
            {
                linkLabel1.Links.Add(match.Index, match.Length, match.Value);
            }
        }


        public event ScreenNameClickedHandler ScreenNameClicked;
        public delegate void ScreenNameClickedHandler(object sender, ScreenNameClickedEventArgs e);
        public event LinkClickedHandler LinkClicked;
        public delegate void LinkClickedHandler(object sender, LinkClickedEventArgs e);
        public event PictureClickHandler PictureClick;
        public delegate void PictureClickHandler(object sender, EventArgs e);


        public class ScreenNameClickedEventArgs : EventArgs
        {
            private string Value;
            public string Name
            {
                get
                {
                    return Value;
                }
            }

            public ScreenNameClickedEventArgs(string name)
            {
                Value = name;
            }
        }
        public class LinkClickedEventArgs : EventArgs
        {
            private string Value;
            public string Url
            {
                get
                {
                    return Value;
                }
            }

            public LinkClickedEventArgs(string url)
            {
                Value = url;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string text = (string)e.Link.LinkData;
            if ((text.IndexOf("http") != -1) && (text.IndexOf("://") != -1) && (LinkClicked != null))
            {
                LinkClickedEventArgs eventarg = new LinkClickedEventArgs(text);
                LinkClicked(this,eventarg);
            }
            else if (ScreenNameClicked != null)
            {
                ScreenNameClickedEventArgs eventarg = new ScreenNameClickedEventArgs(text);
                ScreenNameClicked(this,eventarg);
            }

        }

        private void userPicture_DoubleClick(object sender, EventArgs e)
        {
            PictureClick(this, e);
        }


    }
}
